const multer = require('multer')
const express=require('express')
const router=express.Router()
const path = require('path')


const dest = path.resolve( __dirname , '../../前端/src/assets/images');
//创建磁盘存储引擎
const storage=multer.diskStorage({
    //确定存储路径
    destination:function (req,file,handler){
        handler(null,dest);
    },
    //生成文件名(即保存在服务器上的文件名)
    filename:function (req,file,handler){
        //获取用户上传的原始文件名
        let original=file.originalname
        let prefix = file.originalname;
        let index = prefix.lastIndexOf('.')
        if(index!==-1){
            prefix=prefix.substring(0,index)
        }
        //获取文件拓展名
        let suffix= path.extname(original)
        //将前缀和后缀拼接起来形成新文件名称
        let filename = prefix+suffix

        handler(null,filename)
    }
})
//创建multer实例
const upload = multer({storage:storage})

const  singleFileHandler=upload.single('')
const mySingleHandler=(req,res)=>{
    console.log(req.file.originalname)
    res.send(
        '成功'
    )
}
router.post('/upload',singleFileHandler,mySingleHandler)


module.exports=router